﻿Imports System.Data
Imports MyGeneration.dOOdads
Imports ClassSigeco.ClassSigeco

Public Class controller_stock

    Private _MessageError As Exception = Nothing
    Public Property MessageError() As Exception
        Get
            Return _MessageError
        End Get
        Set(value As Exception)
            _MessageError = value
        End Set
    End Property

#Region "Mercaderias"

    Public Function Insert_mercaderia(conn As String, mercaderia As String, stockMinimo As Integer, idMarca As Integer, idRubro As Integer,
                                       observaciones As String, idImpuesto As Integer, actualizadoPor As Integer) As Integer

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim merca As New si_mercaderia(conn)

            merca.AddNew()
            merca.Mercaderia = mercaderia
            merca.StockMinimo = stockMinimo
            merca.IdMarca = idMarca
            merca.IdRubro = idRubro
            merca.Observaciones = observaciones
            merca.IdImpuesto = idImpuesto
            merca.ActualizadoPor = actualizadoPor
            merca.ActualizadoFecha = DateTime.Now
            merca.Save()

            tx.CommitTransaction()

            Return merca.IdMercaderia

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return 0
        End Try

    End Function

    Public Function Update_mercaderia(conn As String, idMercaderia As Integer,
                                      mercaderia As String, stockMinimo As Integer, idMarca As Integer, idRubro As Integer,
                                       observaciones As String, idImpuesto As Integer, actualizadoPor As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim merca As New si_mercaderia(conn)
            merca.Where.IdMercaderia.Value = idMercaderia

            If merca.Query.Load Then

                merca.Mercaderia = mercaderia
                merca.StockMinimo = stockMinimo
                merca.IdMarca = idMarca
                merca.IdRubro = idRubro
                merca.Observaciones = observaciones
                merca.IdImpuesto = idImpuesto
                merca.ActualizadoPor = actualizadoPor
                merca.ActualizadoFecha = DateTime.Now
                merca.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function Delete_mercaderia(conn As String, idMercaderia As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim merca As New si_mercaderia(conn)
            merca.Where.IdMercaderia.Value = idMercaderia

            If merca.Query.Load Then

                merca.MarkAsDeleted()
                merca.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function get_mercaderias_All(conn As String, idMercaderia As Integer, mercaderia As String,
                                        idMarca As Integer, idRubro As Integer) As DataTable
        Try

            Dim vmercas As New vw_mercaderias(conn)

            If idMercaderia > 0 Then
                vmercas.Where.IdMercaderia.Value = idMercaderia
            End If

            If mercaderia <> "" Then
                vmercas.Where.Mercaderia.Value = "%" & mercaderia & "%"
                vmercas.Where.Mercaderia.Operator = WhereParameter.Operand.Like
            End If

            If idMarca > 0 Then
                vmercas.Where.IdMarca.Value = idMarca
            End If

            If idRubro > 0 Then
                vmercas.Where.IdRubro.Value = idRubro
            End If

            vmercas.Query.AddOrderBy("mercaderia", WhereParameter.Dir.ASC)

            If vmercas.Query.Load Then
                If vmercas.DefaultView.Table.Rows.Count > 0 Then
                    Return vmercas.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function


    Public Function get_marcas(conn As String, idMarca As Integer, marca As String) As DataTable
        Try

            Dim obj As New si_marca(conn)

            If idMarca > 0 Then
                obj.Where.IdMarca.Value = idMarca
            End If

            If marca <> "" Then
                obj.Where.Marca.Value = "%" & marca & "%"
                obj.Where.Marca.Operator = WhereParameter.Operand.Like
            End If

            obj.Query.AddOrderBy("Marca", WhereParameter.Dir.ASC)

            If obj.Query.Load Then
                If obj.DefaultView.Table.Rows.Count > 0 Then
                    Return obj.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_rubros(conn As String, idRubro As Integer, rubro As String) As DataTable
        Try

            Dim obj As New si_rubro(conn)

            If idRubro > 0 Then
                obj.Where.IdRubro.Value = idRubro
            End If

            If rubro <> "" Then
                obj.Where.Rubro.Value = "%" & rubro & "%"
                obj.Where.Rubro.Operator = WhereParameter.Operand.Like
            End If

            If obj.Query.Load Then
                If obj.DefaultView.Table.Rows.Count > 0 Then
                    Return obj.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_impuestos(conn As String, idImpuesto As Integer, impuesto As String) As DataTable
        Try

            Dim obj As New si_impuesto(conn)

            If idImpuesto > 0 Then
                obj.Where.IdImpuesto.Value = idImpuesto
            End If

            If impuesto <> "" Then
                obj.Where.Impuesto.Value = "%" & impuesto & "%"
                obj.Where.Impuesto.Operator = WhereParameter.Operand.Like
            End If

            If obj.Query.Load Then
                If obj.DefaultView.Table.Rows.Count > 0 Then
                    Return obj.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function


    Public Function Insert_marcas(conn As String, marcaDescrip As String) As Integer

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim marca As New si_marca(conn)

            marca.AddNew()
            marca.Marca = marcaDescrip
            marca.Save()

            tx.CommitTransaction()

            Return marca.IdMarca

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return 0
        End Try

    End Function

    Public Function Update_marca(conn As String, idMarca As Integer, marcaDescrip As String) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim merca As New si_marca(conn)
            merca.Where.IdMarca.Value = idMarca

            If merca.Query.Load Then

                merca.Marca = marcaDescrip
                merca.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function Delete_marcas(conn As String, idMarca As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim marca As New si_marca(conn)
            marca.Where.IdMarca.Value = idMarca

            If marca.Query.Load Then

                marca.MarkAsDeleted()
                marca.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function


    Public Function Insert_rubro(conn As String, rubroDescrip As String) As Integer

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim rubro As New si_rubro(conn)

            rubro.AddNew()
            rubro.Rubro = rubroDescrip
            rubro.Save()

            tx.CommitTransaction()

            Return rubro.IdRubro

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return 0
        End Try

    End Function

    Public Function Update_rubro(conn As String, idRubro As Integer, rubroDescrip As String) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim merca As New si_rubro(conn)
            merca.Where.IdRubro.Value = idRubro

            If merca.Query.Load Then

                merca.Rubro = rubroDescrip
                merca.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function Delete_rubro(conn As String, idRubro As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim rubro As New si_rubro(conn)
            rubro.Where.IdRubro.Value = idRubro

            If rubro.Query.Load Then

                rubro.MarkAsDeleted()
                rubro.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function


    Public Function Insert_impuesto(conn As String, impuestoDescrip As String, valor As Decimal) As Integer

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim impuesto As New si_impuesto(conn)

            impuesto.AddNew()
            impuesto.Impuesto = impuestoDescrip
            impuesto.Valor = valor
            impuesto.Save()

            tx.CommitTransaction()

            Return impuesto.IdImpuesto

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return 0
        End Try

    End Function

    Public Function Update_impuesto(conn As String, idImpuesto As Integer, impuestoDescrip As String, valor As Decimal) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim merca As New si_impuesto(conn)
            merca.Where.IdImpuesto.Value = idImpuesto

            If merca.Query.Load Then

                merca.Impuesto = impuestoDescrip
                merca.Valor = valor
                merca.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function Delete_impuesto(conn As String, idImpuesto As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim impuesto As New si_impuesto(conn)
            impuesto.Where.IdImpuesto.Value = idImpuesto

            If impuesto.Query.Load Then

                impuesto.MarkAsDeleted()
                impuesto.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    '------------------------------

    Public Function Insert_deposito(conn As String, descripcion As String, ubicacion As String, idUsuario As Integer) As Integer

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            
            tx.BeginTransaction()

            Dim deposito As New si_deposito(conn)

            deposito.AddNew()
            deposito.Descripcion = descripcion
            deposito.Ubicacion = ubicacion
            deposito.Habilitado = True
            deposito.ActualizadoPor = idUsuario
            deposito.ActualizadoFecha = DateTime.Now
            deposito.Save()

            tx.CommitTransaction()

            Return deposito.IdDeposito

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return 0
        End Try

    End Function

    Public Function Update_deposito(conn As String, idDeposito As Integer,
                                    descripcion As String, ubicacion As String, idUsuario As Integer, habilitado As Boolean) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim deposito As New si_deposito(conn)
            deposito.Where.IdDeposito.Value = idDeposito

            If deposito.Query.Load Then
                deposito.Descripcion = descripcion
                deposito.Ubicacion = ubicacion
                deposito.ActualizadoPor = idUsuario
                deposito.Habilitado = habilitado
                deposito.ActualizadoFecha = DateTime.Now
                deposito.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function Delete_deposito(conn As String, idDeposito As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim deposito As New si_deposito(conn)
            deposito.Where.IdDeposito.Value = idDeposito

            If deposito.Query.Load Then

                deposito.MarkAsDeleted()
                deposito.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function get_depositos(conn As String, idDeposito As Integer,
                                  descripcion As String, ubicacion As String, habilitado As Boolean) As DataTable
        Try

            Dim deposito As New vw_depositos(conn)

            If idDeposito > 0 Then
                deposito.Where.IdDeposito.Value = idDeposito
            Else
                deposito.Where.Habilitado.Value = habilitado
            End If

            If descripcion <> "" Then
                deposito.Where.Descripcion.Value = "%" & descripcion & "%"
                deposito.Where.Descripcion.Operator = WhereParameter.Operand.Like
            End If

            If ubicacion <> "" Then
                deposito.Where.Ubicacion.Value = "%" & ubicacion & "%"
                deposito.Where.Ubicacion.Operator = WhereParameter.Operand.Like
            End If

            If deposito.Query.Load Then
                Return deposito.DefaultView.Table
            Else
                Return Nothing
            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

#End Region

End Class
